//@version=5
indicator("Dorsey Inertia w/ cross", shorttitle="Dorsey", overlay=true)

// Toggle for fixed values
useFixedValue = input.bool(false, title="Use Fixed Value?")

// Input for asset type and timeframe selection
assetType = input.string("forex", title="Asset Type", options=["forex", "crypto", "metals", "stocks"])
timeFrame = input.string("1d", title="Time Frame", options=["4h", "1d"])

// Default inputs for custom settings
AvgPeriod = input.int(title="Average Period", defval=21)
RVIPeriod = input.int(title="RVI Period", defval=14)
SmoothingPeriod = input.int(title="Smoothing Period", defval=14)

// Apply fixed values if useFixedValue is enabled
if useFixedValue
    if assetType == "forex" and timeFrame == "1d"
        RVIPeriod := 3
        AvgPeriod := 2
        SmoothingPeriod := 34
    else if assetType == "forex" and timeFrame == "4h"
        RVIPeriod := 29
        AvgPeriod := 2
        SmoothingPeriod := 47
    else if assetType == "crypto" and timeFrame == "1d"
        RVIPeriod := 4
        AvgPeriod := 19
        SmoothingPeriod := 66
    else if assetType == "crypto" and timeFrame == "4h"
        RVIPeriod := 27
        AvgPeriod := 2
        SmoothingPeriod := 20
    else if assetType == "metals" and timeFrame == "1d"
        RVIPeriod := 16
        AvgPeriod := 18
        SmoothingPeriod := 4
    else if assetType == "metals" and timeFrame == "4h"
        RVIPeriod := 11
        AvgPeriod := 2
        SmoothingPeriod := 32
    else if assetType == "stocks" and timeFrame == "1d"
        RVIPeriod := 6
        AvgPeriod := 2
        SmoothingPeriod := 10
    else if assetType == "stocks" and timeFrame == "4h"
        RVIPeriod := 43
        AvgPeriod := 8
        SmoothingPeriod := 10

// Relative Volatility Index (1993)
rviOriginal(src, AvgPeriod, SmoothingPeriod) =>
    stdev = ta.stdev(src, AvgPeriod)
    upSum = ta.ema(ta.change(src) >= 0 ? stdev : 0, SmoothingPeriod)
    downSum = ta.ema(ta.change(src) >= 0 ? 0 : stdev, SmoothingPeriod)
    100 * upSum / (upSum + downSum)

rviHigh = rviOriginal(high, AvgPeriod, RVIPeriod)
rviLow = rviOriginal(low, AvgPeriod, RVIPeriod)
rvi = (rviHigh + rviLow) / 2
inertia = ta.linreg(rvi, SmoothingPeriod, 0)

// Color and plot settings
inertiaColor = inertia > 50 ? color.green : color.red
plot(inertia, title="dorsey", linewidth=2, color=inertiaColor)
hline(50, title="Middle Level", linestyle=hline.style_dotted, color=color.gray)

// Crossover markers above and below 50 level, displayed at the bottom with tiny size
crossOver = ta.crossover(inertia, 50)
crossUnder = ta.crossunder(inertia, 50)

plotshape(crossOver, title="Crossover Above 50", location=location.bottom, color=color.green, style=shape.triangleup, size=size.tiny)
plotshape(crossUnder, title="Crossover Below 50", location=location.bottom, color=color.red, style=shape.triangledown, size=size.tiny)
